#include "EMPGun.h"
#include "Player.h"
#include "ObjectManager.h"
#include "MessageSystem.h"
#include "CreateProjectileMessage.h"  
#include "GamePlayState.h"
#include "SGD Wrappers\CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_XAudio2.h"

EMPGun::EMPGun(Player* owner)
{
	SetWidth(5);
	SetHeight(10);
	SetTextureID(CSGD_TextureManager::GetInstance()->LoadTexture(_T("Graphics/EMPGun.png")));

	SetMetalPrice(50);
	SetCircuitPrice(100);

	player = owner;
	//owner->AddRef();
	SetX(owner->GetX() + owner->GetWidth());
	SetY(owner->GetY());

	ObjectManager::GetInstance()->AddObject(this);
	Release();

	type = OBJ_WEAPON;
	rangedType = RW_EMP;
	attackReady = false;
	specialReady = false;
	ROF = 1.0f;
	shotTimer = ROF;
	cooldownSeconds = 15.0f;
	cooldownTimer = cooldownSeconds;
	energyCost = 25;
	priceScrap = 50;
	priceCircuit = 100;

	//empShot = CSGD_XAudio2::GetInstance()->SFXLoadSound(_T("Sound/Ranged Weapons/empShot.wav"));

	emitter = nullptr;//Emitter::LoadEmitter("Emitters/empShot.xml");
}

EMPGun::~EMPGun()
{
	if(GetTextureID() != -1 )
	{
		CSGD_TextureManager::GetInstance()->UnloadTexture(GetTextureID());
		SetTextureID(-1);
	}
	//if(empShot != -1)
	//{
	//	CSGD_XAudio2::GetInstance()->SFXStopSound(empShot);
	//	CSGD_XAudio2::GetInstance()->SFXUnloadSound(empShot);
	//	empShot = -1;
	//}

	//emitter->Shutdown();

	ObjectManager::GetInstance()->RemoveObject(this);
}

bool EMPGun::CheckCollision(IEntity* base)
{
	return false;
}

void EMPGun::Attack()
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(EMP, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(empShot);
		//emitter->Burst();
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(EMP_CLOCKED, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//CSGD_XAudio2::GetInstance()->SFXPlaySound(empShot);
		//emitter->Burst();
	}

	attackReady = false;
	shotTimer = 0;
}

void EMPGun::SpecialAttack()
{
	if(GetOwner()->GetClock() == false)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(EMP_BURST, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//emitter->Burst();
	}
	else if(GetOwner()->GetClock() == true)
	{
		CreateProjectileMessage* msg = new CreateProjectileMessage(EMP_BURST, this);
		MessageSystem::GetInstance()->SendMsg(msg);
		msg = nullptr;
		//emitter->Burst();
	}

	specialReady = false;
	cooldownTimer = 0;
}

void EMPGun::Render()
{
	SetX(player->GetX() + player->GetWidth());
	SetY(player->GetY());

	//emitter->SetPosition(GetX(),GetY()+GetHeight());
	//if(emitter->IsBursting())
	//	emitter->Render();

	SetDirection(player->GetDirection());

	RECT image = {};

	switch(GetDirection())
	{
	case 3:
		{
			image.top = 8;
			image.bottom = 13;
			image.left = 6;
			image.right = 10;
			SetWidth(4);
			SetHeight(5);
			SetX(player->GetX() + 35);
			SetY(player->GetY() + 30);
			
		}
		break;

	case 0:
		{
			image.top = 0;
			image.bottom = 5;
			image.left = 0;
			image.right = 10;
			SetWidth(10);
			SetHeight(5);
			SetX(player->GetX() + 15);
			SetY(player->GetY() + 30);
		}
		break;

	case 2:
		{
			image.top = 8;
			image.bottom = 13;
			image.left = 0;
			image.right = 4;
			SetWidth(4);
			SetHeight(5);
			SetX(player->GetX() + 5);
			SetY(player->GetY() + 30);
		}
		break;
	}


	if(player->GetCurWeapon() == 2)
	{
		if(GetTextureID() != -1)
		{
			CSGD_TextureManager::GetInstance()->Draw(GetTextureID(),
			int(GetX() - GamePlayState::GetInstance()->GetCamX()),
			int(GetY() - GamePlayState::GetInstance()->GetCamY()), 2.0f, 2.0f, &image);
		}
	}
}

